<?php
class Zendvn_System_Acl {
	
	protected $_acl;
	protected $_role;
	protected $_groupPrivilege;
	public function __construct($aclInfo=null,$options=null){
		if(!empty($aclInfo)){
			$acl = new Zend_Acl();
			$this->_role = $aclInfo['role'];
			$acl->addRole(new Zend_Acl_Role($this->_role));
			$this->_groupPrivilege = $aclInfo['privilege'];
			$acl->allow($this->_role,null,$this->_groupPrivilege);
			$this->_acl =$acl ;
		}
	}
	public function isAllowed ($arrParam = null){
		$this->_key 	= Zend_Registry::get('keyApp');
		$ns         	= new Zend_Session_Namespace($this->_key);
		$nsInfo     	= $ns->getIterator();
		$flagAccess 	= false;
		$groupPrivilege = $arrParam['module'] . '_'.$arrParam['controller'] .'_'.$arrParam['action'];
		if($this->_acl->isAllowed($this->_role,null,$groupPrivilege)){
			$flagAccess = true;
		}
		return $flagAccess;
	}
	
	public function createPrivilegeArray($options=null){
		$this->_key = Zend_Registry::get('keyApp');
		$ns         = new Zend_Session_Namespace($this->_key);
		$nsInfo     = $ns->getIterator();
		$info       = $nsInfo['member'];
		$group_id   = $info['group_id'];
		$db         = Zend_Registry::get('connectDB');
		$select     = $db->select()
					     ->from('privileges as p')
					     ->join('user_group_privileges as gp','gp.privilege_id = p.id')
					     ->where('status = 1')
					     ->where('group_id = ?',$group_id,INTEGER);
		$result     = $db->fetchAll($select);
		if(!empty($result)){
			$arrPrivilege = array();
			foreach ($result as $key){
			$arrPrivilege [] = $key['module'] .'_'.$key['controller'].'_'.$key['action'];
			}
		}
		$ns->acl['privilege']=$arrPrivilege;
	
	}
	public function createRole($options=null){
		$this->_key      = Zend_Registry::get('keyApp');
		$ns              = new Zend_Session_Namespace($this->_key);
		$nsInfo          = $ns->getIterator();
		$info            = $nsInfo['group'];
		$group_name      = $info['group_name'];
		$ns->acl['role'] = $group_name;
	}
}